﻿using D.UtilCore;

using D.UtilCore.Dtos;
using D.UtilCore.Filters;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using CozeDto.UserDtos;
using CozeDomain.IServices;
using CozeDto.AdminDtos;
using D.UtilFreesqlOrm;
using System.Security.Cryptography;
using D.Util.UtilSecurity;
using CozeDomain.Services;
using CozeDto.ProjectDtos;
using Microsoft.OpenApi.Writers;

namespace CozeWapApi
{
    /// <summary>
    /// 用户模块
    /// </summary>
    [Route("/api/[controller]/[action]")]
    public class UserController : BaseApiController
    {
        private readonly IUserService _userService;
        public UserController(IUserService userService)
        {
            _userService = userService;
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="reqAddUser"></param>
        /// <returns></returns>
        [HttpPost, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> Add([FromBody] ReqAddUser reqAddUser)
        {
            reqAddUser.CreateUser = Account;
            return await _userService.AddUserAsync(reqAddUser);
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="reqUpdateUser"></param>
        /// <returns></returns>
        [HttpPut("{id}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> Update(string id, [FromBody] ReqUpdateUser reqUpdateUser)
        {
            if (id == LoginId)
            {
                reqUpdateUser.Id = id;
                reqUpdateUser.UpdateUser = Account;
                return await _userService.UpdateUserAsync(reqUpdateUser);
            }
            return new ApiResult<int> { Code = StatusCodeEnum.Params, Msg = "参数有误" };
        }

        /// <summary>
        /// 修改支付二维码
        /// </summary>
        /// <param name="reqUpdateUser"></param>
        /// <returns></returns>
        [HttpPut("{id}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> SavePaymentQRCode(string id, [FromBody] ReqUpdatePaymentQRCode reqUpdateUser)
        {
            if (id == LoginId)
            {
                reqUpdateUser.Id = id;
                reqUpdateUser.UpdateUser = Account;
                return await _userService.UpdateUserPaymentQRCodeAsync(reqUpdateUser);
            }
            return new ApiResult<int> { Code = StatusCodeEnum.Params, Msg = "参数有误" };
        }


        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="reqUpdatePwd"></param>
        /// <returns></returns>
        [HttpPut("{id}"), TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<int>> UpdatePwd(string id, [FromBody] ReqUpdateUserPwd reqUpdatePwd)
        {
            if (id == LoginId)
            {
                reqUpdatePwd.Id = id;
                reqUpdatePwd.UpdateUser = Account;
                return await _userService.UpdatePwdAsync(reqUpdatePwd);
            }
            return new ApiResult<int> { Code = StatusCodeEnum.Params, Msg = "参数有误" };
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <returns></returns>
        [HttpGet, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<RepUserInfo>> Get()
        {
            return await _userService.GetUserAsync(LoginId);
        }

        /// <summary>
        /// 获取当前账户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<RepUserAccount>> GetAccount()
        {
            return await _userService.GetUserAccountAsync(LoginId);
        }


        /// <summary>
        /// 获取当前账户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet, TypeFilter(typeof(ExtActionFilterAttribute))]
        [Authorize]
        public async Task<ApiResult<List<RepUserTeam>>> QueryMyTeam()
        {
            return await _userService.QueryMyTeamAsync(LoginId);
        }
    }
}
